package com.sf.guava.basic;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;

/**
 * @Classname OptionsNullTest
 * @Date 2019/12/6 0006 17:47
 * @Created by 埔枘
 */
public class OptionsNullTest {

    public static void main(String[] args) {
//        Integer value1 =  null;
//        Integer value2 =  new Integer(10);
//        Optional<Integer> a = Optional.fromNullable(value1);
//        Optional<Integer> b = Optional.of(value2);
//        System.out.println(sum(a,b));
    }
    public static Integer sum(Optional<Integer> a, Optional<Integer> b){
        System.out.println("First parameter is present: " + a.isPresent());
        System.out.println("Second parameter is present: " + b.isPresent());
        Integer value1 = a.or(new Integer(0));
        Integer value2 = b.get();
        return value1 + value2;
    }

    /**
     *  返回一个 空的 Optional 对象
     */
    public static void absentTest(){
        Optional<Integer> b1 = Optional.absent();
        Optional<String> b2 = Optional.absent();
        Optional<List> b3 = Optional.absent();
        System.out.println(b1.isPresent());
        System.out.println(b2.isPresent());
        System.out.println(b3.isPresent());
    }

    /**
     *  为空快速失败
     */
    public static void ofTest(){
        Integer value2 =  null;
        Optional<Integer> b = Optional.of(value2);
        System.out.println(b);
    }
    /**
     *  将 null 引用的对象 转换成 Optional对象
     */
    public static void fromNullAble(){
        Integer value2 =  null;
        Optional<Integer> b = Optional.fromNullable(value2);
        System.out.println(b.isPresent());
    }


    public static void select(){
        Optional optional = Optional.of("123");
        //判断是否为 null，
        System.out.println(optional.isPresent());
        //获取值
        System.out.println(optional.get());
        // 不为空返回 值，为空 返回默认值
        System.out.println(optional.or("this is default"));
        //不为空返回 值，为空返回 null
        System.out.println(optional.orNull());
        // 返回含有值的 Set 集合
        System.out.println(optional.asSet());
        // 转换成 用户想要的对象类型
        Optional transform = optional.transform(new Function() {
            @Nullable
            @Override
            public Object apply(@Nullable Object input) {
                return input+"ok";
            }
        });
        System.out.println(transform.get());
        // 转成 java 提供的 Options 对象
        System.out.println(optional.toJavaUtil().get());
    }



}
